﻿using System.Linq.Expressions;

namespace In66.Net.Elasticsearch
{
    public static class ElasticsearchBuilderExtesion
    {
        /// <summary>
        /// Match
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field">要查询的列</param>
        /// <param name="value">要查询的关键字</param>
        /// <param name="boost"></param>
        public static ElasticsearchBuilder<T> Match<T>(this ElasticsearchBuilder<T> builder, string field,
            bool condition,
            string value, double? boost = null) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Match(mq => mq.Field(field).Query(value).Boost(boost)));

            return builder;
        }

        public static ElasticsearchBuilder<T> Match<T>(this ElasticsearchBuilder<T> builder,
            bool condition, Expression<Func<T, object>> field, string value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Match(mq => mq.Field(field).Query(value)));
            return builder;
        }

        /// <summary>
        /// MultiMatch
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="fields">要查询的列</param>
        /// <param name="value">要查询的关键字</param>
        public static ElasticsearchBuilder<T> MultiMatch<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string[] fields, string value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.MultiMatch(mq => mq.Fields(fields).Query(value)));

            return builder;
        }

        /// <summary>
        /// MultiMatch
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="fields">例如：f=>new [] {f.xxx, f.xxx}</param>
        /// <param name="value">要查询的关键字</param>
        public static ElasticsearchBuilder<T> MultiMatch<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> fields,
            string value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.MultiMatch(mq => mq.Fields(fields).Query(value)));

            return builder;
        }

        /// <summary>
        /// 大于
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field">要查询的列</param>
        /// <param name="value">要比较的值</param>
        public static ElasticsearchBuilder<T> GreaterThan<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).GreaterThan(value)));

            return builder;
        }

        public static ElasticsearchBuilder<T> GreaterThan<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).GreaterThan(value)));

            return builder;
        }

        /// <summary>
        /// 大于等于
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field">要查询的列</param>
        /// <param name="value">要比较的值</param>
        public static ElasticsearchBuilder<T> GreaterThanEqual<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).GreaterThanOrEquals(value)));

            return builder;
        }

        public static ElasticsearchBuilder<T> GreaterThanEqual<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).GreaterThanOrEquals(value)));

            return builder;
        }

        /// <summary>
        /// 小于
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field">要查询的列</param>
        /// <param name="value">要比较的值</param>
        public static ElasticsearchBuilder<T> LessThan<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).LessThan(value)));

            return builder;
        }

        public static ElasticsearchBuilder<T> LessThan<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).LessThan(value)));
            return builder;
        }

        /// <summary>
        /// 小于等于
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field">要查询的列</param>
        /// <param name="value">要比较的值</param>
        public static ElasticsearchBuilder<T> LessThanEqual<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).LessThanOrEquals(value)));
            return builder;
        }

        public static ElasticsearchBuilder<T> LessThanEqual<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> field,
            double value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Range(mq => mq.Field(field).LessThanOrEquals(value)));
            return builder;
        }

        /// <summary>
        ///一个Term，一个列一个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field">要查询的列</param>
        /// <param name="value">要比较的值</param>
        public static ElasticsearchBuilder<T> Term<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string field,
            object value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Term(field, value));
            return builder;
        }

        public static ElasticsearchBuilder<T> Term<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> field, object value) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Term(field, value));
            return builder;
        }

        /// <summary>
        /// 一个Terms，一个列多个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="musts"></param>
        /// <param name="field"></param>
        /// <param name="values"></param>
        public static ElasticsearchBuilder<T> Terms<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            string field,
            object[] values) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Terms(tq => tq.Field(field).Terms(values)));
            return builder;
        }

        public static ElasticsearchBuilder<T> Terms<T>(this ElasticsearchBuilder<T> builder,
            bool condition,
            Expression<Func<T, object>> field,
            object[] values) where T : ElasticsearchEntity
        {
            if (condition)
                builder.FuncList.Add(d => d.Terms(tq => tq.Field(field).Terms(values)));
            return builder;
        }
    }
}
